package com.hxzy.controller.admin;

import com.hxzy.dto.PageDTO;
import com.hxzy.entity.TbNewbeeMallAdminUser;
import com.hxzy.service.TbNewbeeMallAdminUserService;
import com.hxzy.vo.BootstrapTableVO;
import com.hxzy.vo.ResultEnum;
import com.hxzy.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

@Controller
@RequestMapping(value = "/admin")
public class TbNewbeeMallAdminUserController {
    @Autowired
    private TbNewbeeMallAdminUserService tbNewbeeMallAdminUserService;

    @GetMapping(value = "/adminuser")
    private ModelAndView search(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("admin/adminuser");

        mv.addObject("path","adminuser");

        return mv;
    }

    @ResponseBody
    @GetMapping(value = "/adminuser/data")
    public ResultVO ajaxDate(PageDTO pageDTO){
        BootstrapTableVO search = this.tbNewbeeMallAdminUserService.search(pageDTO);

        return ResultVO.success(search);
    }

    @ResponseBody
    @PostMapping(value = "/adminuser/save")
    public ResultVO addOrUpdate(@Valid TbNewbeeMallAdminUser tbNewbeeMallAdminUser, BindingResult bindingResult){
        if (bindingResult.getFieldErrorCount()>0){
            String msg = bindingResult.getFieldErrors().get(0).getDefaultMessage();
            ResultVO failed = ResultVO.failed(ResultEnum.PARAMS_FAILED);
            failed.setMessage(msg);
            return failed;
        }

        return this.tbNewbeeMallAdminUserService.addOrUpdate(tbNewbeeMallAdminUser);
    }
}
